<?php
    $event      = $data['Event'];
    $members    = $data['Member'];
    $guests     = $data['Guest'];
    $chronicles = $data['Chronicle'];
    $parent     = $data['EventParent'];
    $program    = $data['EventsRepertoire'];
    
    $parentLabel = '';
    if ($parent['id']) {
        $parentLabel = sprintf("%s (%s - %s)", $parent['name'], 
                                   date_local($parent['start']), 
                                   date_local($parent['end']) );
    }
?>


<?= $this->element('back') ?>

<? if((int)@$event['id']>0): ?>
	<h1><?= strtotitle(__('edit')) ?> <?= __('of event') ?></h1>
	<p>
	    <?= $this->Html->link(strtotitle(__('singers list')), '/events/singers/' . $event['id'], array('class'=>'popup')) ?> |
	    <?= $this->Html->link(strtotitle(__('travellers list')), '/events/travellers/' . $event['id'], array('class'=>'popup')) ?>
	</p>
<? else: ?>
	<h1><?= strtotitle(__('new')) . ' ' . __('event') ?></h1>
<? endif; ?>

<?= $this->Form->create('Event', array('action'=>'save', 'enctype'=>'multipart/form-data')) ?>
	<?= $this->App->input('id', 'hidden', @$event['id']) ?>
	
	<p><?= $this->App->input('parent_id', 'autocomplete', @$event['parent_id'], array(
        'url'          => '/events/parents/' . @$event['id'],
        'class'        => 'large',
        'label'        => 'main event',
        'labelOptions' => array('class' => 'inline'),
        'autocomplete_value' => $parentLabel
    )) ?></p>
    
	<p><?= $this->App->input('name', 'text', @$event['name'], array('class' => 'large')) ?></p>
	
	<p><?= $this->App->input('type', 'select', @$event['type'], array(
        'options'     => $eventTypes,
        'options_raw' => true
    )) ?></p>
	
	<p>
	    <?= $this->App->input('start', 'date', @$event['start'], array('label' => 'from')) ?>
	    <?= $this->App->input('end', 'date', @$event['end'], array(
            'label' => 'to',
            'labelOptions' => array( 'class'=>'inline auto' )
	    )) ?>
	</p>
	
	<p><?= $this->App->input('conductor_id', 'select', @$event['conductor_id'], array(
        'options' => $conductors,
        'label'   => 'conductor',
        'options_raw' => true
	)) ?>
	
	<p><?= $this->App->input('description', 'textarea', @$event['description']) ?>
	
	<br clear="all">
	<fieldset>
	    <legend><?= __('members') ?></legend>
	    <? 
	        if (empty($members)) {
	            $members = array(array());
            }
            
	    ?>
	    <? foreach ($voices as $voice): ?>
	    <div class="left span30p">
	        <strong><?= __($voice) ?>:</strong><br>
	        
	        <?php
                if (empty($membersByVoice[$voice])) {
                    $membersByVoice[$voice] = array(array());
                }
	        ?>
	        
	        <div class="multi-inputs">
	            <? foreach ($membersByVoice[$voice] as $id=>$name): ?>
    	        <span class="multi-input">
    	        <?= $this->Form->select("EventsMember.member_id.", $allMembersByVoice[$voice], array(
                    'empty' => false,
	                'value' => $id
    	        )) ?>
    	        </span>
    	        <? endforeach ?>
	        </div>
	    </div>
	    <? endforeach ?>
	    <!-- div class="multi-inputs">
            <? foreach ($members as $k=>$member): ?>
            <span class="multi-input span30p left">
                <?= $this->Form->select("EventsMember.member_id.", $allMembers, array(
                    'empty' => false,
                    'value' => @$member['id']
                )) ?>
            </span>
            <? endforeach ?>
	    </div-->
	</fieldset>
	<fieldset>
	    <legend><?= __('guests') ?></legend>
	    <? 
	        if (empty($guests)) {
	            $guests = array(array());
            }
            
	    ?>
	    <div class="multi-inputs">
            <? foreach ($guests as $k=>$guest): ?>
            <span class="multi-input span30p left">
                <?= $this->Form->select("EventsGuest.guest_id.", $allGuests, array(
                    'empty' => false,
                    'value' => @$guest['id']
                )) ?>
            </span>
            <? endforeach ?>
	    </div>
	</fieldset>
	
    <fieldset>
        <legend><?= __('program') ?></legend>
        <? if (empty($program)) {
            $program = array(array());
        } ?>
        
        <div class="multi-inputs full" id="program">
            <? $k=0 ?>
            <? foreach ($program as $repertoireId=>$p): ?>
                <div class="multi-input left separated">
                    <?= $this->Form->select('EventRepertoire.repertoire_id.', $repertoire, array(
                        'value' => @$repertoireId,
                        'empty' => false,
                        'class' => 'large'
                    )) ?>
                    <br clear="all">
                    <?= $this->element('event_repertoire_members', array(
                        'index' => $k,
                        'data'  => $p
                    )) ?>
                </div>
            <? $k++ ?>
            <? endforeach ?>
        </div>
    </fieldset>
	
	<fieldset>
	    <legend><?= __('chronicles') ?></legend>
	    <? 
	        if (empty($chronicles)) {
	            $chronicles = array(array());
            }
            
	    ?>
	    <div class="multi-inputs full" id="chronicles">
            <? foreach ($chronicles as $k=>$chronicle): ?>
            <span class="multi-input left">
                <?= $this->Form->text("Chronicle.title.", array(
                    'value' => @$chronicle['title'],
                    'class' => 'large'
                )) ?>
                <?= $this->Form->hidden("Chronicle.file.", array(
                    'value' => @$chronicle['file']
                )) ?>
                <? if (! empty($chronicle['file'])): ?>
                    <span class="left" style="margin:5px">
                        <?= $this->Html->image(icon($chronicle['file']), array(
                            'url'   => '/img/chronicles/'.$event['id']."/".$chronicle['file'],
                            'title' => $chronicle['file']
                        )) ?>
                    </span>
                <? endif ?>
                <?= $this->Form->file("Chronicle.newFile.") ?>
            </span>
            <? endforeach ?>
	    </div>
	</fieldset>
	
<?= $this->Form->end(strtotitle(__('save'))) ?>
<br clear="all">
<script type="text/javascript">
$(function(){
	chroniclesSortable();
});
function chroniclesSortable() {
    $('#chronicles .multi-input').each(function(){
        $(this).find('.handle').remove();
        $(this).append('<span class="handle"></span>')
    });
	
	$('#chronicles').sortable({
		items: '.multi-input',
		forcePlaceholderSize: true,
		revert: 'invalid',
		placeholder: 'ui-sortable-placeholder left',
		handle: '.handle'
	});
}
function chronicles_onAdd(item) {
	chroniclesSortable()
}
function chronicles_onAdd(list) {
	chroniclesSortable()
}
function program_onAdd(item) {
    $('#program').children('.multi-input').each(function(index){
        $(this).find('.multi-input').each(function(){
            $(this).find('input[type=hidden]').val('');
            $(this).find('select,input').each(function(){
                var name = $(this).attr('name');
                $(this).attr('name', name.replace(/\[\d+\]/, '['+index+']'));
            });
        });
    });
}
</script>
